package giftmail.mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;

import giftmail.model.Email;

/**
 * MQ的消息监听器
 * @author Demo
 *
 */
public class ConsumerMessageListener implements MessageListener {

	private MessageConverter messageConverter; 
	
	//接受到消息会自动调用该方法
	@Override
	public void onMessage(Message message) {

		if (message instanceof ObjectMessage) {  
            ObjectMessage objMessage = (ObjectMessage) message;  
            try {  
//                Object obj = objMessage.getObject();  
//                Email email = (Email) obj;  
            	Email email = (Email) messageConverter.fromMessage(objMessage); 
                System.out.println("接收到一个ObjectMessage，包含Email对象。");  
                System.out.println(email);  
            } catch (JMSException e) {  
                e.printStackTrace();  
            }  
        }
		
	}

	public MessageConverter getMessageConverter() {
		return messageConverter;
	}

	public void setMessageConverter(MessageConverter messageConverter) {
		this.messageConverter = messageConverter;
	}

}
